package com.bjxst.web.servlet;

import com.bjxst.pojo.Users;
import com.bjxst.service.UserManagerService;
import com.bjxst.service.impl.UserManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*处理用户管理请求*/
@WebServlet("/userManager.do")
public class UserManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);//解决405异常
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //对传入来的字符串进行判断  是添加用户还是查询用户
        //先做一个标记  在前端页面加标记  <input thype=“hidden” name="flag" value="addUser"/>
        String flag = req.getParameter("flag");
        if("addUser".equals(flag)){
            this.addUser(req,resp);
        }else if("finduser".equals(flag)){
            this.findUser(req,resp);
        }else if("preUpdate".equals(flag)){
            this.preUpdate(req,resp);
        }else if("modifyUser".equals(flag)) {
            this.modifyUser(req, resp);
        }else {
            this.dropUser(req,resp);
        }
    }

    //处理添加用户请求
    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //取到新创建的用户
        Users users = this.createUsers(req);
        try{
            //业务层的操作
            UserManagerService userManagerService = new UserManagerServiceImpl();
            userManagerService.addUser(users);
            //添加完成后，要跳转到另一个页面，否则会容易重复添加  用重定向
            resp.sendRedirect("ok.jsp");

        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }
    //获取用户数据
    private Users createUsers(HttpServletRequest req){
        //从form表单中获取数据
        String username = req.getParameter("username");
        String userpwd = req.getParameter("userpwd");
        String usersex = req.getParameter("usersex");
        String phonenumber = req.getParameter("phonenumber");
        String qqnumber = req.getParameter("qqnumber");
        //把数据放进Users对象中
        Users user = new Users();
        user.setUsername(username);
        user.setUserpwd(userpwd);
        user.setUsersex(usersex);
        user.setPhonenumber(phonenumber);
        user.setQqnumber(qqnumber);
        return user;

    }

    //用户查询
    private void findUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        Users user = this.createUsers(req);
        try{
            //创建业务层的对象  把在前端得到的user传到业务层 再传到持久层完成查询
            UserManagerService userManagerService = new UserManagerServiceImpl();
            List<Users> list = userManagerService.findUser(user);
            req.setAttribute("list",list);
            req.getRequestDispatcher("usermanager/viewUser.jsp").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }

    //预更新查询
    private void preUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //先取得用户输入的id
        String userid = req.getParameter("userid");
        //将拿到时的id传到业务层
        try{
            //实例化业务层
            UserManagerService userManagerService = new UserManagerServiceImpl();
            //userid要强转为int
            Users user = userManagerService.findUserById(Integer.parseInt(userid) );
            req.setAttribute("user",user);
            req.getRequestDispatcher("usermanager/updateUser.jsp").forward(req,resp);

        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error,jsp");
        }
    }

    //更新用户
    private void modifyUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String userid = req.getParameter("userid");
        //取到用户
        Users users = this.createUsers(req);
        //因为createUsers(req) 方法中没有userid 而提交用户后需要有userid
        users.setUserid(Integer.parseInt(userid));
        try{
            //实例化业务层
            UserManagerService userManagerService = new UserManagerServiceImpl();
            userManagerService.modifyUser(users);
            // 重定向  跳转到另一页面  更新一次就可以，不用重复更新 所以不用请求转发
            resp.sendRedirect("ok.jsp");
        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }

    //删除用户
    private void dropUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //删除用户是根据idn删除，所以先取用户id  现传到业务层
        String userid = req.getParameter("userid");
        try{
            UserManagerService userManagerService = new UserManagerServiceImpl();
            userManagerService.dropUser(Integer.parseInt(userid));
            //删除是DML操作  所以用重定向  跳转到另一页面
            resp.sendRedirect("ok.jsp");
        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error,jsp");
        }
    }
}
